package com.bilibili.juc.cf.one;

import cn.hutool.core.util.RandomUtil;

import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/*
当发生异常时，handler也会执行，当每个handler遇到异常时继续抛出。那么程序就会以抛出异常的形式终止
 */
public class FutureHandler4Test {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        CompletableFuture<String> orgFuture = CompletableFuture.supplyAsync(
                ()->{
                    System.out.println("supplyAsync 当前线程名称：" + Thread.currentThread().getName());
                    try {
                        Thread.sleep(2000L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    int count = RandomUtil.randomInt(1, 10);
                    System.out.println("随机数为: " + count);
                    if (count < 7){
                        throw new RuntimeException("第一个任务抛出了异常");
                    }
                    return "捡田螺的小男孩";
                }
        );

        CompletableFuture<String> handler1Future = orgFuture.handle((a, throwable) -> {
            System.out.println("handle1 当前线程名称：" + Thread.currentThread().getName());
            if (Objects.isNull(throwable)){
                System.out.println("上个任务执行完啦，还把 \"" + a + "\" 传过来");
                if ("捡田螺的小男孩".equals(a)) {
                    System.out.println("111");
                    return a+"==>>handler1关注了";
                }
                System.out.println("222");
                return a+"==>>handler1再考虑一下";
            }else {
                System.out.println("*** handler1处理异常信息： " + throwable.getMessage());
                throw new RuntimeException("handler1捕获到了异常，继续抛出："+throwable.getMessage());
            }
        });
        CompletableFuture<String> handler2Future = handler1Future.handle((a, throwable) -> {
            System.out.println("handle2 当前线程名称：" + Thread.currentThread().getName());
            if (Objects.isNull(throwable)){
                System.out.println("上个任务执行完啦，还把 \"" + a + "\" 传过来");
                if ("捡田螺的小男孩".equals(a)) {
                    System.out.println("333");
                    return a+"==>>handler2关注了";
                }
                System.out.println("444");
                return a+"==>>handler2再考虑一下";
            }else {
                System.out.println("*** handler2处理异常信息： " + throwable.getMessage());
                throw new RuntimeException("handler2捕获到了异常，继续抛出："+throwable.getMessage());
            }
        });

        System.out.println("handler1Future结果："+handler1Future.get());
        System.out.println("handler2Future结果："+handler2Future.get());
        for (int i = 0 ; i < 5;i++){
            System.out.println(" 主程序正在执行其它的任务"+i);
            TimeUnit.SECONDS.sleep(1);
        }
    }
}
/*
supplyAsync 当前线程名称：ForkJoinPool.commonPool-worker-1
随机数为: 6
handle1 当前线程名称：ForkJoinPool.commonPool-worker-1
*** handler1处理异常信息： java.lang.RuntimeException: 第一个任务抛出了异常
handle2 当前线程名称：ForkJoinPool.commonPool-worker-1
*** handler2处理异常信息： java.lang.RuntimeException: handler1捕获到了异常，继续抛出：java.lang.RuntimeException: 第一个任务抛出了异常
Exception in thread "main" java.util.concurrent.ExecutionException: java.lang.RuntimeException: handler1捕获到了异常，继续抛出：java.lang.RuntimeException: 第一个任务抛出了异常
	at java.base/java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:396)
	at java.base/java.util.concurrent.CompletableFuture.get(CompletableFuture.java:2073)
	at com.bilibili.juc.cf.one.FutureHandler4Test.main(FutureHandler4Test.java:65)
Caused by: java.lang.RuntimeException: handler1捕获到了异常，继续抛出：java.lang.RuntimeException: 第一个任务抛出了异常
	at com.bilibili.juc.cf.one.FutureHandler4Test.lambda$main$1(FutureHandler4Test.java:46)
	at java.base/java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:934)
	at java.base/java.util.concurrent.CompletableFuture$UniHandle.tryFire(CompletableFuture.java:911)
	at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510)
	at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1773)
	at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1760)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182)
	at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)
 */
